package com.itheima.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.itheima.domain.entity.Shop;
import com.itheima.domain.vo.Result;
import com.itheima.service.IShopService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 坤哥
 * @since 2025-10-06
 */
@RestController
@RequestMapping("/shop")
@Slf4j
public class ShopController {
    @Resource
    private IShopService shopService;

    /**
     * 把热门店铺存入到redis（逻辑过期）
     */
    @PostMapping("/save/logic")
    public Result saveShopToRedisLogic(@RequestParam Long shopId,@RequestParam Long expireTime){
        log.info("要保存的店铺id为：{},设置保存的时间为：{}",shopId,expireTime);
        return shopService.saveShopToRedisLogic(shopId,expireTime);
    }
    /**
     * 根据店铺id查询店铺信息  /  并发访问热点店铺
     */
    @GetMapping("/{shopId}")
    public Result queryShopById(@PathVariable("shopId") Long shopId){
        log.info("要查询的店铺id为：{}",shopId);
        return shopService.queryShopById(shopId);
    }
    /**
     * 发布笔记时模糊分页查询店铺列表
     */
    @GetMapping("/of/name")
    public Result queryShopList(@RequestParam(value = "name",required = false) String name,
                                @RequestParam(value = "current",defaultValue = "1") Integer current){
        log.info("要查询的店铺名称为：{},查询的页码为：{}",name,current);
        return shopService.queryShopList(name,current);
    }
    /**
     * 根据商铺类型分页查询商铺信息
     */
    @GetMapping("/of/type")
    public Result queryShopByType(
            @RequestParam("typeId") Integer typeId,
            @RequestParam(value = "current", defaultValue = "1") Integer current
    ) {
        Page<Shop> page = shopService.query().eq("type_id",typeId)
                .page(new Page<>(current, 10));
        return Result.ok(page.getRecords());
    }
}
